package com.lma.crm.classes.domain.web.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.lma.crm.classes.domain.CrmClass;
import com.lma.crm.classes.service.CrmClassService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class CrmClassAction implements ModelDriven<CrmClass> {

    private CrmClass crmClass = new CrmClass();
    
    @Override
    public CrmClass getModel() {
        return crmClass;
    }

    private CrmClassService crmClassService;
    public void setCrmClassService(CrmClassService crmClassService) {
        this.crmClassService = crmClassService;
    }
    public CrmClassService getCrmClassService() {
        return crmClassService;
    }
    //////////////////////////////////////////////////////////////////////
    
    public String findAll() {
        
        System.out.println(crmClassService);
        
        List<CrmClass> allClass = this.crmClassService.findAll();
        
        ActionContext.getContext().put("allClass", allClass);
        
        return "findAll";
    }
    
    public String uploadUI() {
        
        CrmClass crmClass = this.crmClassService.findById(this.crmClass.getClassId());
        
        ActionContext.getContext().getValueStack().push(crmClass);
        
        return "uploadUI";
    }
    
    
    ////////////////////////////////////////////////////////////////////////////
    /*
     * 课表上传
     */
    private File schedule;//上传文件内容
    private String scheduleFileName;//上传文件名
    private String scheduleContentType;//文件类型

    public void setSchedule(File schedule) {
        this.schedule = schedule;
    }
    public void setScheduleFileName(String scheduleFileName) {
        this.scheduleFileName = scheduleFileName;
    }
    public void setScheduleContentType(String scheduleContentType) {
        this.scheduleContentType = scheduleContentType;
    }
    
    @InputConfig(resultName="uploadError")
    public String upload() throws IOException {
        
        if(schedule == null) {
            return "uploadError";
        }
        
        //上传目录
        String paraentDir = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
        //上传文件名
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //上传文件到制定目的地
        
        FileUtils.copyFile(schedule, new File(paraentDir, fileName));
        
        this.getModel().setUploadFilename(scheduleFileName);
        this.getModel().setUploadTime(new Date());
        this.getModel().setUploadPath("/WEB-INF/upload/" + fileName);
        
        this.crmClassService.updateUpload(this.getModel());
        
        return "upload";
    }
    
}
